স্প্রিং বুট ক্লায়েন্টে Performance Optimization করার জন্য বিভিন্ন পদ্ধতি ব্যবহার করা যায়। এর মধ্যে রয়েছে caching, asynchronous processing, connection pooling, এবং efficient data serialization। নিচে উদাহরণসহ একটি পূর্ণাঙ্গ গাইড দেওয়া হলো:
স্প্রিং বুট ক্লায়েন্টে ক্যাশিং ব্যবহার করে বারবার একই ডেটা পুনরুদ্ধার এড়ানো যায়।
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
@EnableCaching
অ্যানোটেশন ব্যবহার করে ক্যাশিং সক্রিয় করুন।
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class CacheConfig {
}
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiClient {
private final RestTemplate restTemplate;
public ApiClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Cacheable("posts")
public String fetchPost(int postId) {
String url = "https://jsonplaceholder.typicode.com/posts/" + postId;
return restTemplate.getForObject(url, String.class);
}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
private final ApiClient apiClient;
public ApiController(ApiClient apiClient) {
this.apiClient = apiClient;
}
@GetMapping("/posts/{id}")
public String getPost(@PathVariable int id) {
return apiClient.fetchPost(id);
}
}
INFO - Cache hit for key: posts::1
@Async
ব্যবহার করে asynchronous কাজ করা সম্ভব।<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@EnableAsync
ব্যবহার করে asynchronous প্রসেসিং সক্রিয় করুন।
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAsync
public class AsyncConfig {
}
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.CompletableFuture;
@Service
public class AsyncApiClient {
private final RestTemplate restTemplate;
public AsyncApiClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Async
public CompletableFuture<String> fetchData(String url) {
String response = restTemplate.getForObject(url, String.class);
return CompletableFuture.completedFuture(response);
}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.CompletableFuture;
@RestController
public class AsyncController {
private final AsyncApiClient asyncApiClient;
public AsyncController(AsyncApiClient asyncApiClient) {
this.asyncApiClient = asyncApiClient;
}
@GetMapping("/async-fetch")
public CompletableFuture<String> fetchAsync() {
String url = "https://jsonplaceholder.typicode.com/posts/1";
return asyncApiClient.fetchData(url);
}
}
RestTemplate
বা WebClient
-এ Connection Pooling ব্যবহার করা হলে একই সার্ভারে একাধিক রিকোয়েস্ট দ্রুত পাঠানো সম্ভব।
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(20);
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(requestFactory);
}
}
ডিফল্টভাবে, স্প্রিং Jackson
ব্যবহার করে JSON সিরিয়ালাইজেশন করে। পারফরম্যান্স উন্নত করার জন্য কাস্টমাইজেশন করুন।
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
return objectMapper;
}
}
স্প্রিং অ্যাকচুয়েটর ব্যবহার করে অ্যাপ্লিকেশনের মেট্রিক্স মনিটরিং করুন।
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
management.endpoints.web.exposure.include=*
Actuator Endpoint:http://localhost:8080/actuator/metrics
এই পদ্ধতিগুলি একত্রে ব্যবহার করলে স্প্রিং বুট ক্লায়েন্টে উল্লেখযোগ্য পারফরম্যান্স উন্নতি করা সম্ভব।
Read more